<?php
session_start();
require_once( 'course_db.php' );
require_once('employee_db.php');

$m_db = new M_Course();
$m_emp = new M_Employee();
$user_id = $_SESSION['userid'];
function MonthThai($m){
 $thaimonth=array("ม.ค.","ก.พ.","มี.ค.","เม.ย.","พ.ค."," มิ.ย.","ก.ค.","ส.ค.","ก.ย.","ต.ค.","พ.ย.","ธ.ค.");
  
 return $thaimonth[$m];
}
function WeekDate($m){
	$_date=array("จ.","อ.","พ.","พฤ.","ศ.","ส.","อา.");
	
	return $_date[$m];
}

 
if (isset($_POST['m'])  &&  $_POST['m']!=""){
    if ($_POST['m']== 'load'){
	
	  $_y = $_POST['y'];
	  $_t = $_POST['t'];
	  $_s = $_POST['s'];
	  $detail=$_POST['d'];
	  
      $result=$m_db->loadData($_y,$_t,$_s,$detail,0);
      $rows=$m_db->getRowCount();
      $i=1;
      $html="";
      if ($rows>0){
           $data = array();
            while($row = $result->FetchRow()){
			   $re_detail=$m_db->load_detail_data($row['id']);
			   $study_date= "";
			   $study_class="";
			   $study_time="";
			   $start_date = "";
			   $end_date="";
			   while($row_detail = $re_detail->FetchRow()){
					$list_class =  $m_db->getClasslist($row_detail['for_class']);
					$list_date = $m_db->getDate($row_detail['study_date']); 
					$study_class .= "<span style='display:block;'>". $list_class ."</span>" ;
					$study_date .= "<span style='display:block;'>". $list_date ."</span>" ;
					$study_time .=" <span style='display:block;'>".$row_detail['study_time'].'-'.$row_detail['study_time_end']."</span>";
					$start_date .="<span style='display:block;'>".$row_detail['start_day']. " ". MonthThai($row_detail['start_month']-1)." ". ($row_detail['start_year'] + 43) ."</span>";
					$end_date .="<span style='display:block;'>".$row_detail['end_day']. " ". MonthThai($row_detail['end_month']-1)." ". ($row_detail['end_year'] + 43)."</span>";
			   }
			   
			   $price = number_format(  $row['cost'],0,'.',','). " บ. / ". $row['type']." <span style='display:block;'>".$row['remark']."</span>";
               $data[] = array(0,$row['id']
								, $row['subject_name'].'<br/>'.$row['detail']
								, $study_class
								, $study_date
								, $study_time 
								, $start_date  
								, $price
								, $row['status']) ;
            }
        }else{
			$data = array();
        }
    
        $arr = array('aaData'=>$data);
        
        echo json_encode($arr);
		
    }else if ($_POST['m']=='detail'){
		$result=$m_db->loadData("","",0,"",$_POST['id']);
		$row = $result->FetchRow();
		echo json_encode($row);
	
	}else if ($_POST['m']=='load_detail'){
		$cid= $_POST['id'];
		$result=$m_db->load_detail_data($cid);
		$rows=$m_db->getRowCount();
		$i=1;
		
        $data = array();
		if ($rows>0){
            while($row = $result->FetchRow()){
				$list_class=  $m_db->getClasslist($row['for_class']);
				$list_date = $m_db->getDate($row['study_date']);
               $data[] = array(0 
				, $row['for_class']
				, $row['study_date']
				, $row['study_time']
				, $row['study_time_end']
				, $list_class
				, $list_date 
				, $row['study_time'].'-'.$row['study_time_end'].' น.'
				, $row['startdate']
				, $row['enddate']) ;
            }
        }else{
			$data = array();
        }
    
        $arr = array('aaData'=>$data);
        
        echo json_encode($arr);
	
   
	}else if ($_POST['m']=='add'){
        $ar = array();
		$val = $_POST['list'];
		$ar = $_POST;
		$ar['createdate'] =date( "Y/m/d H:i:s" );
		$ar['createby'] = $user_id;
		
		$id=$m_db->insertCourse($ar);
		if (count($val)> 0){
			foreach($val as $row){
				list($d,$m,$y) = explode('/', $row['start_date']);
				list($d1,$m1,$y1) = explode('/', $row['end_date']);
				$row['start_date'] = $y.'/'.$m.'/'.$d;
				$row['end_date'] = $y1.'/'.$m1.'/'.$d1;
				
				$row['course_id'] = $id;
				$m_db->insertCourseDetail($row);
			}
    	}
    }else if ($_POST['m']=='edit'){
		$ar = array();
		$val = $_POST['list'];
		$ar = $_POST;
		$ar['updatedate'] =date( "Y/m/d H:i:s" );
		$ar['updateby'] = $user_id;
		
		$m_db->updateData($_POST['id'],$ar);
		
		if (count($val)> 0){
			foreach($val as $row){
				list($d,$m,$y) = explode('/', $row['start_date']);
				list($d1,$m1,$y1) = explode('/', $row['end_date']);
				$row['start_date'] = $y.'/'.$m.'/'.$d;
				$row['course_id'] = $_POST['id'];
				$row['end_date'] = $y1.'/'.$m1.'/'.$d1;
				$m_db->insertCourseDetail($row);
			}
    	}
	}else if ($_POST['m']=='update_course_fee'){
		$ar['fix1'] = $_POST['fix1'];
		$ar['fix2'] = $_POST['fix2'];
		$ar['fix3'] = $_POST['fix3'];
		$ar['fix4'] = $_POST['fix4'];
		$ar['fix5'] = $_POST['fix5'];
		$ar['fix6'] = $_POST['fix6'];
		
		$m_db->updateFixdata($ar);
		
	}else if ($_POST['m']=='load_fee'){
	
		echo json_encode($m_db->loadFee());
	}
	else if ($_POST['m']=='del'){
         $id=$_POST['id'];
		// $type->setTable($_POST['tb']);  
		 $m_db->deleData($id);
		 
    }
}else{

	if (isset($_GET['m']) && $_GET['m'] !="" ){
		if ($_GET['m'] == 'subject'){
			echo json_encode( $m_db->getDropdownSubject());
		}else if ($_GET['m']=='c'){
			echo  json_encode( $m_db->getDropdownCarDetail($_GET['i']));
		}else if ($_GET['m']=='assets'){
			echo  json_encode($m_db->getAssetsPayment());
		}else if ($_GET['m']=='employee'){
			echo  json_encode($m_emp->getEmployee());
		}else if ($_GET['m'] =='year'){
		  echo json_encode($m_db->getYear_Term());
		}else if ($_GET['m'] =='curryear'){
			echo $m_db->getCurrentYear();
		}else if ($_GET['m'] =='currterm'){
			echo $m_db->getCurrentTerm();
		}else if ($_GET['m']=='class'){
			echo  json_encode($m_db->getClass());
		}else if ($_GET['m']=='cbocourse'){
			echo  json_encode($m_db->getCourse_CBO());
		}else if ($_GET['m']=='feeType'){
			echo  json_encode($m_db->getPaymentTypeService());
		}else if ($_GET['m'] =='term'){
		  echo json_encode($m_db->getTerm());
		}
	} 
   
}
 
?>